/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package VISTA;
import MODELO.*;
import CONTROL.*;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JTextField;
/**
 *
 * @author wdiaz
 */
public class parcial2 extends javax.swing.JDialog {
java.util.ArrayList mia,rl2,rl3,rl4;
    int semaforo = 0;
    /**
     * Creates new form parcial2
     */
    public parcial2(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
         
       
        mia = new java.util.ArrayList();
        rl2 = new java.util.ArrayList();
        rl3 = new java.util.ArrayList();
        rl4 = new java.util.ArrayList();
        
        
        TransBDVendor.cargar(jComboBox1, mia,rl4);
        TransBDCliente.cargar(jComboBox2, rl2,rl3);
        
        
        System.out.println(mia);
        System.out.println(rl2);
        
        
        
        TransBDFactura.consultarTodos();
        if(ServicioBD.siguiente()){
            Factura x=TransBDFactura.get();
             muevaloalaventana(x);
        }
        else
            System.out.println("OCURRIO ALGO RARO NO ESPERADO");
        
    
    }
    
    
    public void muevaloalaventana(Factura m){
        
        jTextField1.setText(m.getFecha());
        jTextField2.setText(m.getNrofactura()+"");
        
        jComboBox1.setSelectedItem(m.getNrovendedor());
        jComboBox2.setSelectedItem(m.getIdcliente());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jButton10 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton1.setText("Primero");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(32, 24, -1, -1));

        jButton2.setText("Anterior");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(119, 24, -1, -1));

        jButton3.setText("Siguiente");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(196, 24, -1, -1));

        jButton4.setText("Ultimo");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(279, 24, -1, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 540, 60));

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setText("Nro factura");
        jPanel2.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 33, -1, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel3.setText("FACTURA");
        jPanel2.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 7, -1, -1));

        jLabel4.setText("Fecha");
        jPanel2.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 76, -1, -1));

        jTextField1.setText(" ");
        jPanel2.add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(106, 73, 414, -1));

        jTextField2.setText("1");
        jPanel2.add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(106, 30, 414, -1));

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CLI0001" }));
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });
        jPanel2.add(jComboBox2, new org.netbeans.lib.awtextra.AbsoluteConstraints(106, 124, 122, -1));

        jLabel5.setText("Cliente: id ");
        jPanel2.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 127, -1, -1));

        jTextField3.setText(" ");
        jPanel2.add(jTextField3, new org.netbeans.lib.awtextra.AbsoluteConstraints(246, 124, 120, -1));

        jTextField4.setText(" ");
        jPanel2.add(jTextField4, new org.netbeans.lib.awtextra.AbsoluteConstraints(382, 124, 138, -1));

        jLabel6.setText("Apellido");
        jPanel2.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 110, -1, -1));

        jLabel7.setText("Nombre");
        jPanel2.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 110, -1, -1));

        jLabel8.setText("Vendedor : Nro");
        jPanel2.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 160, -1, -1));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "VEN001" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jPanel2.add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 160, 120, -1));

        jTextField5.setText(" ");
        jPanel2.add(jTextField5, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 160, 120, -1));

        jTextField6.setText(" ");
        jPanel2.add(jTextField6, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 160, 140, -1));

        jButton10.setText("VER TODAS LAS FACTURAS ");
        jPanel2.add(jButton10, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 200, -1, -1));

        jLabel9.setText("Al dar click se veran todas las facturas en un JTable");
        jPanel2.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 230, -1, -1));

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 110, 540, 280));

        jPanel4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton5.setText("Adicionar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jPanel4.add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 22, -1, -1));

        jButton6.setText("Modificar");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jPanel4.add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 20, -1, -1));

        jButton7.setText("Borrar");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jPanel4.add(jButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 20, -1, -1));

        jButton8.setText("Buscar");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });
        jPanel4.add(jButton8, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 20, -1, -1));

        jButton9.setText("Salir");
        jPanel4.add(jButton9, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 20, -1, -1));

        getContentPane().add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 400, 540, 70));

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 550, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 480, 550, 20));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setText("GESTION DE VENTAS ");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 480, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed

    String scodigo = jTextField2.getText();
        int cod  = Integer.parseInt(scodigo);
        
        String fecha  = jTextField1.getText();
        
        String vendedor =(String)jComboBox1.getSelectedItem();
        String cliente  =(String)jComboBox2.getSelectedItem();
       
        Factura  ci =new Factura(cod, cliente, vendedor, fecha);
        
        
        int n=jComboBox1.getSelectedIndex();
       String coditipoproducion= (String)mia.get(n);
        jTextField5.setText(coditipoproducion);
        
        int m=jComboBox2.getSelectedIndex();
        String codicnnocimiento = (String)rl2.get(m);
        jTextField3.setText(codicnnocimiento);
        
        
        if(TransBDFactura.buscar(cod)){
           javax.swing.JOptionPane.showMessageDialog(this, " YA EXISTE");
           return;
        }
        if(TransBDFactura.adicionar(ci))
         javax.swing.JOptionPane.showMessageDialog(this, "  INGRESADA EXITOSAMENTE");
        else
           javax.swing.JOptionPane.showMessageDialog(this, " NO SE PUDO ADICIONAR"); 
    
    // TODO add your handling code here:
}//GEN-LAST:event_jButton5ActionPerformed

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed

    String scodigo = jTextField2.getText();
    int codigo = Integer.parseInt(scodigo);
     
    if (TransBDFactura.buscar(codigo)){
         TransBDFactura.borrar(codigo);
         System.out.println("factura BORRADA CON EXITO");
     }
     else 
           javax.swing.JOptionPane.showMessageDialog(this, "NO EXISTE LA CIUDAD ANIMAL");
         
    
    
    
    
    
    // TODO add your handling code here:
}//GEN-LAST:event_jButton7ActionPerformed

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed

    String scodigo = jTextField2.getText();
        int codigo  = Integer.parseInt(scodigo);
        String fecha = jTextField1.getText();
        String vendedor =(String)jComboBox1.getSelectedItem();
        String cliente =(String)jComboBox2.getSelectedItem();
        
       Factura  ci=new Factura(codigo, cliente, vendedor, fecha);
        
       int n=jComboBox1.getSelectedIndex();
       String coditipoproucion= (String)mia.get(n);
        jTextField5.setText(coditipoproucion);
        
        int m=jComboBox2.getSelectedIndex();
        String codicnnocimiento = (String)rl2.get(m);
        jTextField3.setText(codicnnocimiento);
      
        
        
        
        if(!TransBDFactura.buscar(codigo)){
           javax.swing.JOptionPane.showMessageDialog(this, " NO EXISTE");
           return;
        }
        if(TransBDFactura.modificar(ci))
         javax.swing.JOptionPane.showMessageDialog(this, "  MODIFICADA EXITOSAMENTE");
        else
           javax.swing.JOptionPane.showMessageDialog(this, " NO SE PUDO MODIFICAR");
    
    
    
    // TODO add your handling code here:
}//GEN-LAST:event_jButton6ActionPerformed

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed

    
    String scodigo = jTextField2.getText();
    int codigo = Integer.parseInt(scodigo);
  if (TransBDFactura.buscar(codigo)){
  Factura x = TransBDFactura.get();
       if (x!=null)
          muevaloalaventana(x);
       else
           javax.swing.JOptionPane.showMessageDialog(this, "NO LO ENCONTRO");
    }
    else
           javax.swing.JOptionPane.showMessageDialog(this, "NO LO ENCONTRO");  
      
    
    // TODO add your handling code here:
}//GEN-LAST:event_jButton8ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

    if (ServicioBD.primero()){
       Factura x= TransBDFactura.get();
       if (x!=null)
          muevaloalaventana(x);
       else
           javax.swing.JOptionPane.showMessageDialog(this, "NO HAY MAS");
    }
    else
           javax.swing.JOptionPane.showMessageDialog(this, "NO HAY MAS");
    
    
    // TODO add your handling code here:
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

    
    
    if (ServicioBD.ultimo()){
       Factura x= TransBDFactura.get();
       if (x!=null)
          muevaloalaventana(x);
       else
           javax.swing.JOptionPane.showMessageDialog(this, "NO HAY MAS");
    }
    else
           javax.swing.JOptionPane.showMessageDialog(this, "NO HAY MAS");
    
    
    
    
    // TODO add your handling code here:
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

    
      if (ServicioBD.siguiente()){
       Factura  x= TransBDFactura.get();
       if (x!=null)
          muevaloalaventana(x);
       else
           javax.swing.JOptionPane.showMessageDialog(this, "NO HAY MAS");
       muevaloalaventana(x);
}
    
    
    
    
    // TODO add your handling code here:
}//GEN-LAST:event_jButton3ActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed

    
    if (ServicioBD.anterior()){
       Factura x= TransBDFactura.get();
       if (x!=null)
          muevaloalaventana(x);
       else
           javax.swing.JOptionPane.showMessageDialog(this, "NO HAY MAS");
    }
    else
           javax.swing.JOptionPane.showMessageDialog(this, "NO HAY MAS");
    // TODO add your handling code here:
}//GEN-LAST:event_jButton4ActionPerformed

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed

    
    
    if(semaforo==1){
            int n=jComboBox1.getSelectedIndex();
            String vendedor= (String)mia.get(n);
            jTextField5.setText(vendedor);
    
            String vend= (String)rl4.get(n);
            jTextField6.setText(vend);
        }
        semaforo = 1;
// TODO add your handling code here:
}//GEN-LAST:event_jComboBox1ActionPerformed

private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed

    if(semaforo==2){
            int n=jComboBox2.getSelectedIndex();
            String cliente= (String)rl2.get(n);
            jTextField4.setText(cliente);
            
            
            String client= (String)rl3.get(n);
            jTextField3.setText(client);

        }
        semaforo = 2;

    
    // TODO add your handling code here:
}//GEN-LAST:event_jComboBox2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(parcial2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(parcial2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(parcial2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(parcial2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                parcial2 dialog = new parcial2(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    // End of variables declaration//GEN-END:variables
}
